/*
 * ComerciosReportesDetallesFrame.java
 *
 * Created on 7 de mayo de 2008, 03:05 PM
 */
package org.contribuyentes.reportes;

import org.mybeans.Sesion;
import org.mybeans.ContribuyentesBeans;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;
import org.contribuyentes.ContribuyentesBusquedaFrame;

/**
 *
 * @author  avbravo
 */
public class ContribuyentesReportesRemolquesFrame extends javax.swing.JFrame {

    Sesion sesion;
    ContribuyentesBeans contribuyentesBeans;
    String MunicipioVive;
    String CorregimientoVive;
    String Corregimiento;
boolean focoContribuyente = false;
    /** Creates new form ComerciosReportesDetallesFrame */
    public ContribuyentesReportesRemolquesFrame() {
        initComponents();
        sesion = new Sesion();
        contribuyentesBeans = new ContribuyentesBeans();

            /*
         * Fondo
         */

         
         
         

         
         
        /*
         * fondo
         */

              if (sesion.getCedulaContribuyente() == null || sesion.getCedulaContribuyente().equals(""))
        {

        }
         else{
            jTextFieldCedula.setText(sesion.getCedulaContribuyente());
            ValidarCedula();
         }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButtonVistaPrevia = new javax.swing.JButton();
        jButtonLimpiar = new javax.swing.JButton();
        jButtonRegresar = new javax.swing.JButton();
        jButtonImprimir = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldCedula = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldContribuyente = new javax.swing.JTextField();
        jButtonBuscarContribuyentes = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Reporte de Remolques del Contribuyente");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButtonVistaPrevia.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/preview.png"))); // NOI18N
        jButtonVistaPrevia.setToolTipText("Vista Previa");
        jButtonVistaPrevia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVistaPreviaActionPerformed(evt);
            }
        });

        jButtonLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/escoba.png"))); // NOI18N
        jButtonLimpiar.setToolTipText("Limpiar");
        jButtonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimpiarActionPerformed(evt);
            }
        });

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Regresar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jButtonImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/print.jpeg"))); // NOI18N
        jButtonImprimir.setToolTipText("Imprimir");
        jButtonImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonImprimirActionPerformed(evt);
            }
        });

        jLabel1.setText("Cedula");

        jTextFieldCedula.setColumns(35);
        jTextFieldCedula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldCedulaActionPerformed(evt);
            }
        });

        jLabel2.setText("Contribuyente");

        jTextFieldContribuyente.setColumns(70);
        jTextFieldContribuyente.setEditable(false);

        jButtonBuscarContribuyentes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/search.png"))); // NOI18N
        jButtonBuscarContribuyentes.setToolTipText("Buscar");
        jButtonBuscarContribuyentes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarContribuyentesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(jButtonVistaPrevia, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(58, 58, 58)
                                .addComponent(jTextFieldCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButtonBuscarContribuyentes, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextFieldContribuyente, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(65, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextFieldCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButtonBuscarContribuyentes)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldContribuyente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(48, 48, 48)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonVistaPrevia, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonVistaPreviaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVistaPreviaActionPerformed
 

    String Cedula = this.jTextFieldCedula.getText();
    if (Cedula == null || Cedula.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese la cedula ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldCedula.requestFocus();
        return;
    }

    String contribuyente = this.jTextFieldContribuyente.getText();
    if (contribuyente == null || contribuyente.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese el contribuyente", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldContribuyente.requestFocus();
        return;
    }

    String reportSource;
    reportSource = sesion.getRutaReportes() + "ContribuyentesRemolques.jrxml";
    String reportDest;
    reportDest = sesion.getRutaDestino() + "ContribuyentesRemolques.jrxml";

    Map<String, Object> params = new HashMap<String, Object>();

    try {
        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        JasperReport jasperReport =
                JasperCompileManager.compileReport(reportSource);
        params.put("P_IDMUNICIPIO", sesion.getIdmunicipio());
        params.put("P_CEDULA", Cedula);
        params.put("P_MUNICIPIO", sesion.getMunicipio());

        JasperPrint jasperPrint =
                JasperFillManager.fillReport(
                jasperReport, params, sesion.con);

        JasperViewer.viewReport(jasperPrint, false);

    } catch (JRException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonVistaPreviaActionPerformed

private void jButtonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimpiarActionPerformed
 
    Limpiar();
}//GEN-LAST:event_jButtonLimpiarActionPerformed

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed
 
    dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jButtonImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonImprimirActionPerformed
 

    String Cedula = this.jTextFieldCedula.getText();
    if (Cedula == null || Cedula.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese la cedula ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldCedula.requestFocus();
        return;
    }

    String contribuyente = this.jTextFieldContribuyente.getText();
    if (contribuyente == null || contribuyente.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese el contribuyente", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldContribuyente.requestFocus();
        return;
    }

    String reportSource;
    reportSource = sesion.getRutaReportes() + "ContribuyentesRemolques.jrxml";
   

    Map<String, Object> params = new HashMap<String, Object>();

    try {
        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        JasperReport jasperReport =
                JasperCompileManager.compileReport(reportSource);
        params.put("P_IDMUNICIPIO", sesion.getIdmunicipio());
        params.put("P_CEDULA", Cedula);
        params.put("P_MUNICIPIO", sesion.getMunicipio());
        JasperPrint jasperPrint =
                JasperFillManager.fillReport(
                jasperReport, params, sesion.con);

        JasperPrintManager.printReport(jasperPrint, true);

    } catch (JRException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonImprimirActionPerformed

private void jButtonBuscarContribuyentesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarContribuyentesActionPerformed
 
    ContribuyentesBusquedaFrame contribuyentesBusquedaFrame = new ContribuyentesBusquedaFrame();
    contribuyentesBusquedaFrame.setLocationRelativeTo(null);
    contribuyentesBusquedaFrame.setVisible(true);
    focoContribuyente = true;
}//GEN-LAST:event_jButtonBuscarContribuyentesActionPerformed

private void jTextFieldCedulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldCedulaActionPerformed
ValidarCedula();
    
}//GEN-LAST:event_jTextFieldCedulaActionPerformed

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
    // TODO add your handling code here:
    try {

            if (focoContribuyente) {
                CargarContribuyentes();
            }

        focoContribuyente = false;
    } catch (Exception ex) {
        sesion.MostrarMensajeError("Error ", "Clase: PagosContribuyentesEventualesFrame.java\nBoton Actualizar Impuestos()\n " + ex.getMessage().toString());
    }
}//GEN-LAST:event_formWindowGainedFocus

private void ValidarCedula(){
    try {
        String cedula = this.jTextFieldCedula.getText();
        sesion.setCedulaContribuyente(jTextFieldCedula.getText());
        if (contribuyentesBeans.Consultar(cedula) == true) {
            CargarContribuyentes();
        }


    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ContribuyentesReportesRemolquesFrame().setVisible(true);
            }
        });
    }

    private void CargarComercios() {
        try {

            this.jTextFieldCedula.setText(contribuyentesBeans.getCedula());
            this.jTextFieldContribuyente.setText(contribuyentesBeans.getContribuyente());

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void Limpiar() {
        this.jTextFieldCedula.setText("");
        this.jTextFieldContribuyente.setText("");
    }

    public void CargarContribuyentes() {
        try {
            // ContribuyentesBeans contribuyentesBeans = new ContribuyentesBeans();


            this.jTextFieldCedula.setText(contribuyentesBeans.getCedula());
            sesion.setCedulaContribuyente(jTextFieldCedula.getText());
            this.jTextFieldContribuyente.setText(contribuyentesBeans.getContribuyente());
            MunicipioVive = BuscarMunicipiosCodigo(contribuyentesBeans.getIdMunicipioVive());
            CorregimientoVive = BuscarCorregimientosCodigo(contribuyentesBeans.getIdMunicipioVive(), contribuyentesBeans.getIdCorregimientoVive());
            Corregimiento = BuscarCorregimientosCodigo(contribuyentesBeans.getIdMunicipio(), contribuyentesBeans.getIdCorregimiento());
            repaint();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private String BuscarCorregimientosCodigo(String idMunicipio, String corregimiento) {
        try {
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return "";
            }
            String sqlquery = "select * from corregimients where idmunicipio = ? and idcorregimiento =?";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            psquery.setString(1, idMunicipio);
            psquery.setString(2, corregimiento);
            ResultSet rs = psquery.executeQuery();
            rs.next();
            int x = rs.getRow();

            if (x == 0) {
                JOptionPane.showMessageDialog(this, "no existe un corregimiento con ese codigo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);

                return "";
            }
            return rs.getString("corregimiento");


        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error Cargando Corregimientos" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        return "";
    }

    private String BuscarMunicipiosCodigo(String municipio) {
        try {
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return "";
            }

            String sqlquery = "select * from municipies where idmunicipio = ? ";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            psquery.setString(1, municipio);

            ResultSet rs = psquery.executeQuery();
            rs.next();
            int x = rs.getRow();

            if (x == 0) {
                JOptionPane.showMessageDialog(this, "no existe un municipio ese codigo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);

                return "";
            }

            return rs.getString("municipio");


        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error Cargando Corregimientos" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        return "";
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBuscarContribuyentes;
    private javax.swing.JButton jButtonImprimir;
    private javax.swing.JButton jButtonLimpiar;
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JButton jButtonVistaPrevia;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextFieldCedula;
    private javax.swing.JTextField jTextFieldContribuyente;
    // End of variables declaration//GEN-END:variables
}
